package com.connection;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class uploadFile {
   // private static final String FTP_SERVER = "https://118.178.197.65:16524/";
// private static final int FTP_PORT = 21;
// private static final String FTP_USERNAME = "fb9pxyye";
// private static final String FTP_PASSWORD = "WJ0826@";
   public static void uploadFile(String address, int port, String username, String password, String email, List<String> fileToSend) {
      FTPClient ftpClient = new FTPClient();
      try {
//连接FTP服务器
         ftpClient.connect(address, port);
//登录FTP服务器
         ftpClient.login(username, password);
//写入方式
         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
         ftpClient.setControlEncoding("UTF-8");
//每次数据连接之前ftp client告诉ftp server开通 一个端口来传输数据。
         ftpClient.enterLocalPassiveMode();

         for (String file : fileToSend) {
            File files = new File(file);
            FileInputStream inputStream = new FileInputStream(files);
// //将流写入服务器
// String savePath = "/www/wwwroot/" + files.getName();
//// boolean uploaded = ftpClient.storeFile(files.getName(), inputStream);
// boolean uploaded = ftpClient.storeFile(savePath, inputStream);
// inputStream.close();
            boolean uploaded = ftpClient.storeFile(files.getName(), inputStream);


            if (uploaded) {
               System.out.println("文件上传成功");
            } else {
               System.out.println("文件上传失败");
            }
         }


      } catch (IOException e) {
         throw new RuntimeException(e);
      } finally {
         try {
            if (ftpClient.isConnected()) {
               ftpClient.logout();
               ftpClient.disconnect();
            }
         } catch (IOException e) {
            throw new RuntimeException(e);
         }
      }
   }

   public static void start(List<String> fileToSend) {
// String path = "D:\\kfm资料\\java\\practice\\servertest1.txt";
// for (String file:fileToSend) {
// uploadFile(file);
//
// }
   }
}